package com.example.vieweventdemo.animotion;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.example.vieweventdemo.R;

/**
 * Created by tangjiangxu on 9/25/17.
 */

public class AnimotionActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animotion);
        final View targetView = findViewById(R.id.tv);
        targetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //ObjectAnimator.ofFloat(targetView, "translationX", 0, 100).setDuration(100).start();
                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) targetView.getLayoutParams();
                params.width += 100;
                params.leftMargin += 100;
                targetView.requestLayout();
                //或者 targetView.setLayoutParams(params)
            }
        });

        //ObjectAnimator.ofFloat(tv, "translationX", 0, 100).setDuration(3000).start();

        //用动画实现View内容滑动
        final int startX = 0;
        final int deltaX = 100;
        ValueAnimator animator = ValueAnimator.ofInt(0, 1).setDuration(2000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float fraction = animation.getAnimatedFraction();
                targetView.scrollTo(startX + (int) (deltaX * fraction), 0);
            }
        });
        animator.start();
    }
}
